Categories
JavaScript Best Practices

JavaScript Best Practices — Variable Names to Avoid

Spread the love

JavaScript is a very forgiving language. It’s easy to write code that runs but has issues in it.

In this article, we’ll look at the kind of names we want to avoid for JavaScript variables.

Avoid Misleading Names or Abbreviations

Is a name is misleading, then we should correct them.

So a variable shouldn’t have double meanings. The variable age should hold the age of something for example.

Avoid Names with Similar Meanings

If names have a similar meaning, then we should avoid them. Something like employeeCode and employeeNum can easily confuse us.

If we have to fix problems, then we’ll run into issues with the confusing that these names bring.

Avoid Variables with Different Meanings but Similar Names

Variable names with different meanings but look similar are also no good.

Something like employeeRecs and employeeReps aren’t good.

The difference in their spelling is only by 1 letter, which can easily be missed.

Avoid Names that Sound Similar

Name that sound similar may also confuse us. Therefore, we should avoid them.

For instance, wrap and rap sound the same, so we can easily confuse the 2 names.

Avoid Numerals in Names

Since numbers are already used for array indexes, we shouldn’t use them for variables names as a suffix.

If we have foo1 and foo2 , then either they aren’t differentiated enough in their name or they should be in the same array.

Avoid Misspelled Words in Names

Misspelled words can easily throw people off, so we should avoid them to avoid any problems with them in the future.

If we have variable names that are misspelled, we should correct them before we go further.

Don’t Differentiate Variable Names Solely by Capitalization

Only differentiating names by capitalization doesn’t differentiate them much.

It’s hard for us to look at names that only differ by this little.

Avoid Multiple Natural Languages

If everyone uses English in the team, then all the names should be in English.

Otherwise, having names in different languages that not everyone may understand will bring problems if they have to read the code.

Make sure everyone knows the language before we name things.

Avoid the Names of Standard Constructors, Variables, and Functions

We should avoid any keywords that are used by the JavaScript language or its standard library.

For instance, we shouldn’t name things with words like, undefined , Array , and things like that.

Don’t Use Names that are Totally Unrelated to What the Variables Represent

If a variable is named for something, then we should make sure that it holds the value that’s given by the name.

So numApples should hold the number of apples for example.

Avoid Names Containing Hard-to-Read Characters

If they’re hard to read characters, then we should avoid them so that everyone can read them easier.

Characters like l and I shouldn’t be used together for example as they both look like a vertical line.

Conclusion

When naming variables, we should make them as easy to understand as possible.

This means that misspellings should be fixed. Names that have a similar meaning, sound, or appearance should also be changed.

Also, we’ve to make sure that everyone knows the language that the variable name is written so that everyone can understand them.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *